home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / dbms_mag / 9103 / lastlog / lastlog.c < prev    next >
C/C++ Source or Header  |  1991-02-10  |  6KB  |  216 lines

  1. /*****************************************************************
  2. *  LastLog.C
  3. *  written by Kathy Cea, Platinum Software Int'l
  4. *
  5. *  LastLog is a utility to list the last login date and time of
  6. *  all network users.
  7. *
  8. *  Calling Syntax:
  9. *     LastLog [N]
  10. *       Include the optional N parameter to suppress
  11. *       page breaks (useful if redirecting output to a file)
  12. *
  13. *   Compiled in Turbo C 2.0 with NetWare C function calls
  14. *
  15. *   Note: This is the complete version of the program included in the
  16. *         Bindery API article in the March 1991 issue of DBMS.  It
  17. *         includes code to format the output.
  18. *
  19. *         Only a Supervisor (or equivalent) can run this program
  20. *         successfully.
  21. *****************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <math.h>
  26. #include <dos.h>
  27. #include <nit.h>
  28. #include <niterror.h>
  29. #define MAXNAME 48
  30.  
  31. void getlastlog (char *);
  32. double today();
  33. int year(), day(), month();
  34. void moremsg();
  35. void newpage();
  36.  
  37. int i,j;
  38. int linecnt = 0;
  39. int maxline = 17;
  40. int pagecnt = 0;
  41. int pagebrk = 1;
  42. double todaycal;
  43. char todaystr[9];
  44. int pagesw;
  45.  
  46. main(int argc, char *argv[]){
  47.  
  48.     WORD objectType;
  49.     char objectName[MAXNAME];
  50.     long objectId;
  51.     int retcode;
  52.     BYTE propertyValue[128];
  53.     BYTE moreSegs,
  54.          propertyFlags;
  55.     int segNum;
  56.     BYTE temp[3];
  57.     BYTE holdobjId[9];
  58.     char *endptr;
  59.     segNum = 1;
  60.     moreSegs = 255;
  61.  
  62.  
  63. /* Check for the [N] switch to indicate "No page breaks" */
  64.     if (argc > 1) {
  65.         pagesw = toupper(argv[1][0]);
  66.         if (pagesw == 'N')
  67.             pagebrk = 0;
  68.         }
  69.  
  70. /* Get today's date and format for printing */
  71.     todaycal = today();
  72.     sprintf(todaystr,"%02d/%02d/%02d",
  73.                     month(todaycal),
  74.                     day(todaycal),
  75.                     year(todaycal));
  76.  
  77. /* Display page header */
  78.     newpage();
  79.  
  80. /* Get all users of group EVERYONE
  81.    Object ID of users are passed back in the propertyValue field
  82.    Up to 32 Object Ids can be returned on each pass.  Continue
  83.    calling ReadPropertyValue until moreSegs is No (0) */
  84.  
  85.     while (moreSegs) {
  86.     retcode = ReadPropertyValue("EVERYONE", OT_USER_GROUP,
  87.                             "GROUP_MEMBERS", segNum,
  88.                             propertyValue, &moreSegs, &propertyFlags);
  89.     segNum++;
  90.  
  91.     /* parse the propertyValue for 4-byte Object IDs,
  92.        then convert each ID to a long integer */
  93.     i = 0;
  94.     temp[2] = '\0';
  95.     holdobjId[0] = '\0';
  96.     while (i < 128) {
  97.         for (j=0; j < 4; j++) {
  98.             /* Build a hex string for each Object ID */
  99.             sprintf(temp, "%02x", propertyValue[i]);
  100.             temp[2] = '\0';
  101.             strcat(holdobjId, temp);
  102.             i++;
  103.         } /* for (j=0; j < 4; j++) */
  104.  
  105.         objectId = strtoul(holdobjId, &endptr, 16);
  106.  
  107.         /* Pass user's Object ID and receive User Name */
  108.         retcode = GetBinderyObjectName(objectId, objectName, &objectType);
  109.         if (!retcode)
  110.             getlastlog(objectName);
  111.         holdobjId[0] = '\0';
  112.     }  /* while (i < 128) */
  113.     } /* while (moreSegs) */
  114. } /* main() */
  115.  
  116. void getlastlog (char objectName[MAXNAME])
  117. /* Gets the last login date & time and the full name for the
  118.     user passed in objectName */
  119.  
  120. {
  121.     WORD objectType;
  122.     int retcode;
  123.     BYTE propertyValue[128];
  124.     BYTE moreSegs,
  125.          propertyFlag;
  126.     char lastlogdt[9];
  127.     char lastlogtm[9];
  128.  
  129.     objectType = OT_USER;
  130.  
  131. /* Read the user's LOGIN_CONTROL property value to obtain the last
  132.    login date & time */
  133.  
  134.     retcode = ReadPropertyValue(objectName, objectType, "LOGIN_CONTROL", 1,
  135.         propertyValue, &moreSegs, &propertyFlag);
  136.     if (!retcode){
  137.         sprintf(lastlogdt,"%02d/%02d/%02d",
  138.                             propertyValue[57],
  139.                             propertyValue[58],
  140.                             propertyValue[56]);
  141.         sprintf(lastlogtm,"%02d:%02d:%02d",
  142.                             propertyValue[59],
  143.                             propertyValue[60],
  144.                             propertyValue[61]);
  145.         lastlogdt[8] = '\0';
  146.         lastlogtm[8] = '\0';
  147.         /* Read the user's IDENTIFICATION property value to obtain the
  148.            full name - Assumes full names no longer than 35 characters */
  149.            retcode = ReadPropertyValue(objectName, objectType, "IDENTIFICATION", 1,
  150.                      propertyValue, &moreSegs, &propertyFlag);
  151.             printf("%-20s",objectName);
  152.             printf("%-35s",propertyValue);
  153.             if (!strcmp(lastlogdt,"00/00/00"))
  154.                 printf("No Logins\n");
  155.             else
  156.                 printf("%-8s\t%-8s\n",lastlogdt, lastlogtm);
  157.             linecnt++;
  158.     } /* if (!retcode) */
  159.     else {
  160.         printf("%-20s",objectName);
  161.         printf("%44s\n","No Logins");
  162.         linecnt++;
  163.     }
  164.     if ((linecnt > maxline) && pagebrk)
  165.         newpage();
  166.     return;
  167. }
  168.  
  169. void newpage()
  170. /* Handle page headers and breaks */
  171. {
  172.     if (pagecnt++ > 0)
  173.         moremsg();
  174.     clrscr();
  175.     printf("                  Last Login Date for All Users\n\n");
  176.     printf("Report Date:  %s                                              Page: %d\n\n",todaystr,pagecnt);
  177.     printf("UserId              Full Name                              Last Login\n");
  178.     printf("------------------------------------------------------------------------------\n");
  179.     linecnt = 0;
  180. }
  181.  
  182. void moremsg()
  183. /* Pause after each screen, and display message */
  184. {
  185.     fprintf(stderr,"Press any key to continue...");
  186.     getch();
  187. }
  188.  
  189. int year(double cal)
  190. /* Return the year portion of a date */
  191. {
  192.         return floor(cal / 10000.0);
  193. }
  194.  
  195. int day(double cal)
  196. /* Return the day portion of a date */
  197. {
  198.         return floor(cal - (floor(cal / 100.0) * 100.0));
  199. }
  200.  
  201. int month(double cal)
  202. /* Return the month portion of a date */
  203. {
  204.     return floor((cal - (year(cal) * 10000.0) - day(cal)) / 100.0);
  205. }
  206.  
  207. double today()
  208. /* Get today's date */
  209. {
  210.     struct date d;
  211.  
  212.     getdate(&d);
  213.     return (d.da_year - 1900) * 10000.0 + d.da_mon * 100.0 + d.da_day;
  214. }
  215.  
  216.